package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.dto.MemberReservationPageDto;
import com.zzyl.nursing.dto.PrameReservationDto;
import com.zzyl.nursing.vo.ReservationCountVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-20
 */
@Slf4j
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> seleReservationList(MemberReservationPageDto reservation) {
        PrameReservationDto prameReservationDto = new PrameReservationDto();
        prameReservationDto.setIndex((reservation.getPageNum() - 1) * 10);
        prameReservationDto.setSize(reservation.getPageSize());
        prameReservationDto.setCreate_by(reservation.getCreate_by());
        prameReservationDto.setStatus(reservation.getStatus());
        //分页查询数据
        List<Reservation> list = reservationMapper.selectReservationlistPage(prameReservationDto);
        return list;
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setStatus(0);
        reservation.setCreateTime(DateUtils.getNowDate());
        reservation.setCreateBy(UserThreadLocal.getUserId().toString());
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询取消预约数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        //获取当前日期
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<Reservation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Reservation::getStatus, 2);
        wrapper.eq(Reservation::getCreateBy, userId);
        //预约时间的格式为：年月日 时分秒，当前日期格式为：年月日，需要预约时间的年月日等于当前日期
        wrapper.apply("date_format(time,'%Y-%m-%d') = date_format('" + now + "','%Y-%m-%d')");
        return Math.toIntExact(count(wrapper));
    }

    /**
     * 查询剩余预约数量
     *
     * @param time
     */
    @Override
    public List<ReservationCountVo> getRemainCount(LocalDateTime time) {
        //查询数据库中的time字段含有该时间段的预约数量
        List<ReservationCountVo> list = reservationMapper.getRemainCount(time);
        return list;
    }

    /**
     * 定时更新预约状态
     */
    @Override
    public void autoupdataStatus() {
        //1.查询状态为0的预约信息 && 预约开始时间小于等于当前时间
        List<Reservation> list = list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now()));
        //2.修改状态为1
        list.forEach(item -> {
            item.setStatus(3);
        });
        //3.批量更新
        updateBatchById(list);
    }
}
